import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Gao zhao xu
 * Date: 2023-09-17
 * Time: 10:34
 */
public class MySingleList {
     static class ListNode{
        public int value;
        public ListNode next;
        public ListNode(int value){
            this.value = value;
        }
    }
    public ListNode head;
//    public void createList(){
//        ListNode node1 = new ListNode(12);
//        ListNode node2 = new ListNode(23);
//        ListNode node3 = new ListNode(34);
//        ListNode node4 = new ListNode(45);
//        ListNode node5 = new ListNode(56);
//        node1.next = node2;
//        node2.next = node3;
//        node3.next = node4;
//        node4.next = node5;
//        this.head = node1;
//    }
    //遍历单链表
    public void show(){
        ListNode cur = head;
        while (cur != null){
            System.out.print(cur.value + " ");
            cur = cur.next;
        }
        System.out.println();
    }
    //得到链表长度
    public int size(){
        int count = 0;
        ListNode cur = head;
        while (cur != null){
            count++;
            cur = cur.next;
        }
        return count;
    }
    //判断链表中是否有key值
    public boolean contains(int key){
        ListNode cur = head;
        while(cur != null){
            //如果value是引用类型，那么需要用equals来进行比较
            if (key == cur.value){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }
    //头插法
        public void  addFirst(int data){
            ListNode node = new ListNode(data);
            node.next = head;
            head = node;
        }
    //尾插法
    public void addLast(int data){
        ListNode node = new ListNode(data);
        if (head == null){
            head = node;
            return;
        }
        ListNode cur = head;
        while(cur.next != null){
            cur = cur.next;
        }
        //cur指向的节点就是尾结点
        cur.next = node;
    }
    //任意位置插入一个结点:第一个节点的序号为0
    public void addIndex(int index, int data){
        //0.判断位置的合法性
        if (index < 0 || index > size()){
            throw new IndexOutBoundsException("获取数值时候，位置不合法");
        }
        if (index == 0){
            addFirst(data);
            return;
        }
        //1.先找到index - 1位置的节点
        ListNode node = new ListNode(data);
        ListNode cur = head;
        for (int i = 0;i < index - 1; i++){
            cur = cur.next;
        }
        //2.进行插入
        node.next = cur.next;
        cur.next = node;
    }

    //删除第一次出现关键字为key的节点
    public void remove(int key){
        if (head == null){
            return;
        }
        if (head.value == key){
            head = head.next;
            return;
        }
        ListNode cur = head;
        while(cur.next.value != key){
            cur = cur.next;
            if (cur.next == null){
                System.out.println("没有你要删除的数据:" + key);
                return;
            }
        }
        cur.next = cur.next.next;
    }
    //删除所有值为key的节点
    public void removeAllKey(int key){
        if (head == null){
            return;
        }
        ListNode cur = head;
        ListNode cur1 = head.next;
        int a = 0;
        while (cur1 != null){
            if (cur1.value == key){
                cur.next = cur1.next;
                a = 1;
            }else{
                cur = cur1;
            }
            cur1 = cur1.next;
        }
        if (head.value == key){
            head = head.next;
            return;
        }
        if (a == 0){
            System.out.println("没有你要删除的数据：" + key);
        }
    }
    //清楚链表
    public void clear() {
        head = null;
    }


    //面试题
    //1.翻转链表
    public  void reverseList() {
        if (head == null) {
            return;
        }
        if (head.next == null) {
            return;
        }
        ListNode cur = head.next;
        head.next = null;
        while (cur != null) {
            ListNode cur1 = cur.next;
            cur.next = head;
            head = cur;
            cur = cur1;
        }
    }
    //1.递归反转链表
    //递归法反转单链表
    public void reverse() {
        //判断当前链表是否为空
        if(head == null || head.next == null) {
            return;
        }
        //不是空就从第一个开始反转
        head = reverse(head);
    }

    //反转单个节点
    private ListNode reverse(ListNode head) {
        //递归的结束条件
        if(head.next==null) {
            return head;
        }
        ListNode new_head = reverse(head.next);
        head.next.next = head;
        head.next = null;
        return new_head;
    }

    //2.链表的中间结点(快慢指针)
    public ListNode middleNode() {
        ListNode fast = head;
        ListNode slow = head;
        while((fast != null && fast.next != null)){
            slow = slow.next;
            fast = fast.next.next;
        }
        return slow;
    }
    //3.倒数第K个节点(快慢指针)
    public ListNode FindKthToTail(int k) {
        if (k <= 0){
            return null;
        }
        ListNode fast = head;
        ListNode slow = head;
        for (int i = 0; i < k; i++){
            if (fast == null){
                return null;
            }
            fast = fast.next;
        }
        while(fast != null){
            slow = slow.next;
            fast = fast.next;
        }
        return slow;
    }
    //4.将两个有序链表合并为一个新的有序链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的(见test)
    //5.链表分割:小于x的放在前面，其余放在后面，且顺序不得改变。
    public ListNode partition( int x) {
        // write code here
        if(head == null){
            return null;
        }
        ListNode cur = head;
        ListNode head1 = new ListNode(0);
        ListNode head2 = new ListNode(0);
        ListNode cur1 = head1;
        ListNode cur2 = head2;
        while(cur != null){
            if (cur.value < x){
                head1.next = cur;
                head1 = head1.next;
            }else{
                head2.next = cur;
                head2 = head2.next;
            }
            cur = cur.next;
        }
        head1.next = cur2.next;
        head2.next = null;
        return cur1.next;
    }
    //6.判断链表是否是回文结构
    public boolean chkPalindrome() {
        if (head == null){
            return false;
        }
        ListNode fast = head;
        ListNode slow = head;
        //找中间位置slow
        while (fast != null && fast.next != null){
            slow = slow.next;
            fast = fast.next.next;
        }
        //开始反转后半链表,翻转完毕后slow为新列表的newHead
        ListNode newHead = slow.next;
        slow.next = null;
        while (newHead != null){
            ListNode cur = newHead.next;
            newHead.next = slow;
            slow = newHead;
            newHead = cur;
        }
        //开始判断是否是回文
        ListNode cur1 = head;
        while(slow != null){
            if (cur1.value != slow .value){
                return false;
            }
            cur1 = cur1.next;
            slow  = slow.next;
        }
        return true;
    }
    //7.返回两个链表的的相同的部分(见test)
    //8.两个元素递增元素的链表A,B，求集合A - B 存放在A链表中。
    public  void subList(ListNode headB){
        if (headB == null){
            return;
        }else if(head == null){
            head = headB;
            return;
        }
        ListNode curA = new ListNode(0);
        curA.next = head;
        head = curA;
        ListNode curB = headB;
        while(curA.next != null){
            int a = 0;
            curB = headB;
            while(curB != null){
                if (curA.next.value == curB.value){
                    a = 1;
                    curA.next = curA.next.next;
                    break;
                }
                curB = curB.next;
            }
            if(a == 0){
                curA = curA.next;
            }
        }
        head = head.next;
    }
    //两个递增链表相减去
    public  void subList1(ListNode headB){
        if (headB == null){
            return;
        }else if(head == null){
            head = headB;
            return;
        }
        ListNode curA = new ListNode(0);
        curA.next = head;
        head = curA;
        ListNode curB = headB;
        while(curA.next != null){
            while(curB != null){
                if (curA.next.value == curB.value){
                    curA.next = curA.next.next;
                    curB = curB.next;
                }else if(curA.next.value < curB.value){
                    curA = curA.next;
                }else{
                    curB = curB.next;
                }
            }
        }
        head = head.next;
    }
    //判断是否有环
    public boolean hasCycle() {
        if (head == null || head.next == null){
            return false;
        }
        ListNode fast = head;
        ListNode slow = head;
        while (fast != null && fast.next != null){
            fast = fast.next.next;
            slow = slow.next;
            if (fast == slow){
                return true;
            }
        }
        return false;
    }
    //找到环的起始点：
    public ListNode detectCycle(){
        if (head == null){
            return null;
        }
        ListNode fast = head;
        ListNode slow = head;
        while(fast != null && fast.next != null){
            fast = fast.next.next;
            slow = slow.next;
            if (fast == slow){
                break;
            }
        }
        if (fast == null || fast.next == null){
            return null;
        }
        fast =head;
        while(fast != slow){
            fast = fast.next;
            slow = slow.next;
        }
        return fast;
    }
}

